package com.atugui.test;

import com.atguigu.config.MainConfigOfProfile;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;

/**
 * 测试@Profile()切换环境
 */
public class IOCTest_Profiler {


    //1.使用命令行动态参数:在虚拟机参数位置加载-Dspring.profiles.active=环境变量名(test)
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext
                = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);

        String[] beanNamesForType = annotationConfigApplicationContext.getBeanNamesForType(DataSource.class);
        for (int i = 0; i < beanNamesForType.length; i++) {
            System.out.println(beanNamesForType[i]);
        }

        annotationConfigApplicationContext.close();
    }

    //2.代码的方式激活
    @Test
    public void test01() {
        //1.创建一个applicationContext
        AnnotationConfigApplicationContext applicationContext
                = new AnnotationConfigApplicationContext();

        //2.设置需要激活的环境
        applicationContext.getEnvironment().setActiveProfiles("test", "dev");
        //3.注册主配置类
        applicationContext.register(MainConfigOfProfile.class);
        //4.启动刷新容器
        applicationContext.refresh();


        String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
        for (int i = 0; i < beanNamesForType.length; i++) {
            System.out.println(beanNamesForType[i]);
        }

        applicationContext.close();
    }


}
